package com.tutk.AV_Player;

import android.annotation.SuppressLint;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Environment;
import android.util.Log;
import android.view.Surface;

import com.tutk.util.MyApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;


/**
 * 视频流解码播放线程
 */
@SuppressLint("NewApi")
public class VideoPlayer extends Thread {
    private MediaCodec decoder;
    private Surface surface;
    public VideoPlayer(Surface surface) {
        this.surface = surface;
    }

    @Override
    public void run() {
        try {

            sleep(500);

            MediaFormat format = MediaFormat.createVideoFormat("video/avc", 1280, 720);
            format.setInteger(MediaFormat.KEY_FRAME_RATE, 16);
            format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 3);
            decoder = MediaCodec.createDecoderByType("video/avc");
            decoder.configure(format, surface, null, 0);

            if (decoder == null) {
                Log.e("DecodeActivity", "Can't find video info!");
                return;
            }

            decoder.start();

        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ByteBuffer[] inputBuffers = decoder.getInputBuffers();
        ByteBuffer[] outputBuffers = decoder.getOutputBuffers();
        MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();

        while (!Thread.interrupted()) {
//            byte[] buf = VideoCall.aCache.getAsBinary("videoBuffer");
            byte[] buf = MyApplication.getVideoBuffer();
            byte[] buf2 = MyApplication.getAudioBuffer();
//            Log.d("system", "获取的共享buffer：" + buf2);
//            saveBuffer(buf2);
            int inIndex = decoder.dequeueInputBuffer(-1);
            Log.d("DecodeActivity", "Index.................." + inIndex);
            if (inIndex >= 0) {
                ByteBuffer inputBuffer = inputBuffers[inIndex];
                inputBuffer.clear();
                inputBuffer.put(buf);

                decoder.queueInputBuffer(inIndex, 0, buf.length, 0, 0);
            }

            int outIndex = decoder.dequeueOutputBuffer(info, 10000);
            while (outIndex >= 0) {
                decoder.releaseOutputBuffer(outIndex, true);
                outIndex = decoder.dequeueOutputBuffer(info, 0);
            }
        }
    }
    private boolean saveBuffer(byte[] data) {
        String strSDPath = Environment.getExternalStorageDirectory().toString();
        final String fileName = "Play.aac";

        if (strSDPath == null || data == null)
            return false;

        StringBuffer path = new StringBuffer(strSDPath);
        path.append('/');
        path.append(fileName);

        try {
            File file = new File(path.toString());
            FileOutputStream fos = new FileOutputStream(file, true);
            fos.write(data, 0, data.length);
            Log.i("print", "saveBuffer....,,,,....");
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}